<template>
  <div>
    <el-card :bordered="false" dis-hover class="ivu-mt">
      <el-form
        ref="tableFrom"
        :model="tableFrom"
        :label-width="labelWidth"
        :label-position="labelPosition"
        @submit.native.prevent
      >
        <el-row type="flex" :gutter="24">
          <!-- <el-form-item label="活动类型：" clearable>
            <el-select
              style="width: 200px"
              v-model="tableFrom.factor"
              placeholder="请选择活动类型"
              clearable
              @on-change="userSearchs"
            >
              <el-option value="1">积分抽取</el-option>
              <el-option value="2">余额</el-option>
              <el-option value="3">订单支付</el-option>
              <el-option value="4">订单评价</el-option>
              <el-option value="5">关注公众号</el-option>
            </el-select>
          </el-form-item> --> 

          <el-form-item label="活动状态：" clearable>
            <el-select
              style="width: 200px"
              v-model="tableFrom.start_status"
              placeholder="请选择"
              clearable
              @on-change="userSearchs"
            >
              <el-option value="0">未开始</el-option>
              <el-option value="1">进行中</el-option>
              <el-option value="-1">已结束</el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="上架状态：">
            <el-select
              style="width: 200px"
              placeholder="请选择"
              v-model="tableFrom.status"
              clearable
              @on-change="userSearchs"
            >
              <el-option value="1">上架</el-option>
              <el-option value="0">下架</el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="抽奖搜索：" label-for="store_name">
            <el-input
              v-model="tableFrom.store_name"
              @keyup.enter.native="getList(1)"
              placeholder="请输入用户信息"
              class="selWidth"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                class="el-button-solt"
                @click="getList(1)"
              />
            </el-input>
          </el-form-item>
        </el-row>

        <!-- <router-link :to="{ path: './create' }">
          <el-button size="small" type="primary">
            <i class="add">+</i> 添加抽奖
          </el-button>
        </router-link> -->
      </el-form>
      <el-table
        :columns="columns1"
        :data="tableList"
        :loading="loading"
        highlight-row
        no-userFrom-text="暂无数据"
        no-filtered-userFrom-text="暂无筛选结果"
      >
        <el-table-column label="ID" min-width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.id }} </span>
          </template>
        </el-table-column>
        <el-table-column label="活动名称" min-width="90">
          <template slot-scope="scope">
            <span>{{ scope.row.name }} </span>
          </template>
        </el-table-column>
        <el-table-column label="活动类型" min-width="130">
          <template slot-scope="scope">
            <span>{{ scope.row.lottery_type }} </span>
          </template>
        </el-table-column>
        <el-table-column label="参与次数" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.lottery_all }} </span>
          </template>
        </el-table-column>
        <el-table-column label="抽奖人数" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.lottery_people }} </span>
          </template>
        </el-table-column>
        <el-table-column label="中奖人数" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.lottery_win }} </span>
          </template>
        </el-table-column>
        <el-table-column label="活动状态" min-width="100">
          <template slot-scope="scope">
            <span>{{ scope.row.status_name }} </span>
          </template>
        </el-table-column>
        <el-table-column label="上架状态" min-width="100">
          <template slot-scope="scope">
            <span slot="open" v-if="scope.row.status == 1">上架</span>
            <span slot="close" v-else>下架</span>
          </template>
        </el-table-column>
        <el-table-column label="活动时间" min-width="100">
          <template slot-scope="scope">
            <div>起：{{ scope.row.start_time || "--" }}</div>
            <div>止：{{ scope.row.end_time || "--" }}</div>
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="150" fixed="right">
          <template slot-scope="scope">
            <router-link :to="{ path: './create?id=' + scope.row.id }">
              <el-button type="text" size="small" class="mr10">编辑</el-button>
            </router-link>

           <!-- <el-button
              type="text"
              size="small"
              @click="handleDelete(scope.row.id)"
              >删除</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
      <div class="acea-row row-right page">
        <el-pagination
          :total="total"
          :current="tableFrom.page"
          show-elevator
          show-total
          @on-change="pageChange"
          :page-size="tableFrom.limit"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { mapState } from "vuex";
import {
  lotteryListApi,
  lotteryStatusApi,
  lucklotteryDelApi,
} from "@/api/lottery";
import { formatDate } from "@/utils/validate";
export default {
  name: "storeBargain",
  filters: {
    formatDate(time) {
      if (time !== 0) {
        let date = new Date(time * 1000);
        return formatDate(date, "yyyy-MM-dd hh:mm");
      }
    },
  },
  data() {
    return {
      loading: false,
      columns1: [
        {
          title: "ID",
          key: "id",
          width: 80,
        },
        {
          title: "活动名称",
          key: "name",
          minWidth: 90,
        },
        {
          title: "活动类型",
          key: "lottery_type",
          minWidth: 130,
        },
        {
          title: "参与次数",
          key: "lottery_all",
          minWidth: 100,
        },
        {
          title: "抽奖人数",
          key: "lottery_people",
          minWidth: 100,
        },
        {
          title: "中奖人数",
          key: "lottery_win",
          minWidth: 100,
        },
        {
          title: "活动状态",
          key: "status_name",
          minWidth: 100,
        },
        {
          title: "上架状态",
          slot: "status",
          minWidth: 100,
        },
        {
          title: "活动时间",
          slot: "time",
          minWidth: 100,
        },
        {
          title: "操作",
          slot: "action",
          fixed: "right",
          minWidth: 130,
        },
      ],
      tableList: [],
      tableFrom: {
        start_status: "",
        status: "",
        store_name: "",
        export: 0,
        page: 1,
        factor: "",
        limit: 15,
      },
      total: 0,
    };
  },
  computed: {
    ...mapState("admin/layout", ["isMobile"]),
    labelWidth() {
      return this.isMobile ? undefined : "80px";
    },
    labelPosition() {
      return this.isMobile ? "top" : "left";
    },
  },
  created() {
    this.getList();
  },
  methods: {
    // 删除
    handleDelete(id) {
      this.$modalSure().then(() => {
        lucklotteryDelApi(id)
          .then(({ message }) => {
            this.$message.success(message);
            this.getList();
          })
          .catch(({ message }) => {
            this.$message.error(message);
          });
      });
    },
    // 添加
    add() {
      this.$router.push({
        path: this.$routeProStr + "/marketing/lottery/create",
      });
    },
    // 编辑
    edit(row) {
      this.$router.push({
        name: "marketing_create",
        query: {
          id: row.id,
        },
      });
    },
    // 一键复制
    copy(row) {
      this.$router.push({
        name: "marketing_create",
        query: {
          id: row.id,
          copy: 1,
        },
      });
    },
    // 删除
    del(row, tit, num) {
      let delfromData = {
        title: tit,
        num: num,
        url: `marketing/lottery/del/${row.id}`,
        method: "DELETE",
        ids: "",
      };
      this.$modalSure(delfromData)
        .then((res) => {
          this.$Message.success(res.msg);
          this.tableList.splice(num, 1);
        })
        .catch((res) => {
          this.$Message.error(res.msg);
        });
    },
    //查看抽奖记录
    getRecording(row) {
      this.$router.push({
        path: this.$routeProStr + `/marketing/lottery/recording_list`,
        query: {
          id: row.id,
        },
      });
    },
    // 列表
    getList() {
      this.loading = true;
      this.tableFrom.start_status = this.tableFrom.start_status || "";
      this.tableFrom.status = this.tableFrom.status || "";
      console.log("this.tableFrom", this.tableFrom);
      lotteryListApi(this.tableFrom)
        .then(async (res) => {
          console.log("res的值", res);
          let data = res.data;
          this.tableList = data.list;
          this.total = res.data.count;
          this.loading = false;
        })
        .catch((res) => {
          this.loading = false;
          this.$Message.error(res.msg);
        });
    },
    pageChange(index) {
      this.tableFrom.page = index;
      this.getList();
    },
    // 表格搜索
    userSearchs() {
      this.tableFrom.page = 1;
      this.getList();
    },
    // 修改是否显示
    onchangeIsShow(row) {
      let data = {
        id: row.id,
        status: row.status,
      };
      lotteryStatusApi(data)
        .then(async (res) => {
          this.$Message.success(res.msg);
          this.getList();
        })
        .catch((res) => {
          this.$Message.error(res.msg);
          this.getList();
        });
    },
  },
};
</script>

<style scoped lang="scss">
.tabBox_img {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  cursor: pointer;

  img {
    width: 100%;
    height: 100%;
  }
}
</style>
